/**
 * @file ui_power_on.c
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2023-06-04
 * 
 * @copyright Copyright (c) 2023
 * 
 */
#include "ui_power_on.h"
#include "msg_event_config.h"
#include "ui_change.h"

static ui_basic_com_t s_ui_power_on = {0};
static bool s_already_init = false;
static lv_obj_t *bg_obj = NULL;
static lv_obj_t *bar = NULL;
LV_FONT_DECLAER(lv_font_montserrat_36);


static int ui_power_on_enter(int arg0, int arg1, void *arg3)
{
    if(s_already_init)
    {
       return -1;
    }

    s_already_init = true;

    lv_disp_t *d_disp = lv_disp_get_default();
    //设置窗体整个的透明度
    //lv_disp_set_bg_opa(d_disp, LV_OPA_TRANSP);

    lv_color_t bg_color = {0};
    bg_color = lv_color_hex(0);
    bg_obj = lv_obj_create(lv_scr_act());
    lv_obj_set_size(bg_obj, d_disp->driver->hor_res, d_disp->driver->ver_res);
    lv_obj_set_pos(bg_obj, 0,  0);
    lv_obj_set_style_bg_color(bg_obj, bg_color, 0);
    lv_obj_set_style_bg_opa(bg_obj, LV_OPA_100, 0);
    lv_obj_set_style_radius(bg_obj, 0, 0);
    lv_obj_set_style_border_width(bg_obj, 0, 0);
    lv_obj_clear_flag(bg_obj, LV_OBJ_FLAG_SCROLLABLE);
    //EasyUI
    lv_obj_t *label = lv_label_create(bg_obj);
    lv_obj_align(label, LV_ALIGN_CENTER, 0, -30);
    lv_label_set_recolor(label,true);
    lv_label_set_text(label, APP_NAME);
    lv_obj_set_style_align(label, LV_ALIGN_CENTER, 0); //set text
    lv_obj_set_style_text_color(label, lv_color_hex(0xFFFFFF), 0);
    lv_obj_set_style_text_font(label, &lv_font_montserrat_36, 0);

    //progress
    bar =  lv_bar_create(bg_obj);
    lv_bar_set_range(bar, 0, 100);
    lv_obj_align(bar, LV_ALIGN_CENTER, 0, 20);
    lv_obj_set_size(bar, d_disp->driver->hor_res*0.7, 6);
    lv_obj_set_style_radius(bar, 0, 0);
    lv_obj_set_style_radius(bar, 0, LV_PART_INDICATOR);
    lv_obj_set_style_bg_color(bar, lv_color_hex(0xFFFFFF), LV_PART_INDICATOR);
    // int bar_x = d_disp->driver->hor_res*0.8; 
    // lv_obj_set_style_x(bar, bar_x, 0);
    return 0;
}

static int ui_power_on_loop(int arg0, int arg1, void *arg3)
{
    if(!s_already_init)
    {
       return -1;
    }

    return 0;
}

static int ui_power_on_exit(int arg0, int arg1, void *arg3)
{
    if(!s_already_init)
    {
       return -1;
    }
    
    s_already_init = false;

    lv_obj_del(bg_obj);

    return 0;
}

static void ui_power_on_event(char *event_name, void *data, int srcId)
{
    if(!s_already_init)
    {
       return;
    }
    
    int up_progress = 0;
    memcpy(&up_progress, data, sizeof(int));
    COMLOG_INFO_LOG("startuo progress: %d\n", up_progress);
    lv_bar_set_value(bar, up_progress, 1);
    if(up_progress == 100)
    {
        ui_change_ui(EASY_UI_HOME, false);
    }
}

void ui_power_on_set_progress(int value)
{
    easy_ui_pub_event(TOPIC_UP_PROGRESS, (void*)&value, sizeof(int), 0);
}

ui_basic_com_t *ui_power_on_create(void)
{
    COMLOG_INFO_LOG("ui_power_on_create\n");
    s_ui_power_on.ui_id = EASY_UI_POWER_ON;
    memcpy(s_ui_power_on.ui_name, EASY_UI_NAME(EASY_UI_POWER_ON), strlen(EASY_UI_NAME(EASY_UI_POWER_ON)));
    s_ui_power_on.enter = ui_power_on_enter;
    s_ui_power_on.run = ui_power_on_loop;
    s_ui_power_on.exit = ui_power_on_exit; 
    easy_ui_sub_event(TOPIC_UP_PROGRESS, ui_power_on_event, ANY_ID);
    return &s_ui_power_on;
}